//分页
let ms = {
    init: function (totalsubpageTmep, args) {
        return (function () {
            ms.fillHtml(totalsubpageTmep, args);
            ms.bindEvent(totalsubpageTmep, args);
        })();
    },
    //填充html
    fillHtml: function (totalsubpageTmep, args) {
        return (function () {
            totalsubpageTmep = "<a class='page_one' href='javascript:void(0);'>首页</a><a class='page_prev'  href='javascript:void(0);'>上一页</a>&nbsp;";
            // 页码大于等于4的时候，添加第一个页码元素
            if (args.currPage != 1 && args.currPage >= 4 && args.totalPage != 4) {
                totalsubpageTmep += "<a class='page_num' href='javascript:;'>" + 1 + "</a>";
            }
            /* 当前页码>4, 并且<=总页码，总页码>5，添加“···”*/
            if (args.currPage - 2 > 2 && args.currPage <= args.totalPage && args.totalPage > 5) {
                totalsubpageTmep += "<a class='page_num_none' href='javascript:;'>...</a>";
            }
            /* 当前页码的前两页 */
            var start = args.currPage - 2;
            /* 当前页码的后两页 */
            var end = args.currPage + 2;

            if ((start > 1 && args.currPage < 4) || args.currPage == 1) {
                end++;
            }
            if (args.currPage > args.totalPage - 4 && args.currPage >= args.totalPage) {
                start--;
            }
            for (; start <= end; start++) {
                if (start <= args.totalPage && start >= 1) {
                    totalsubpageTmep += "<a class='page_num' href='javascript:;'>" + start + "</a>";
                }
            }
            if (args.currPage + 2 < args.totalPage - 1 && args.currPage >= 1 && args.totalPage > 5) {
                totalsubpageTmep += "<a class='page_num_none' href='javascript:;'>...</a>";
            }

            if (args.currPage != args.totalPage && args.currPage < args.totalPage - 2 && args.totalPage != 4) {
                totalsubpageTmep += "<a class='page_num' href='javascript:;'>" + args.totalPage + "</a>";
            }
            totalsubpageTmep += "<a class='page_next' href='javascript:void(0);'>下一页</a><a class='page_last'  href='javascript:void(0);'>尾页</a>&nbsp;<em>共" + args.totalPage + "页&nbsp;</em>";
            $(".page").html(totalsubpageTmep);
        })();
    },
    //绑定事件
    bindEvent: function (totalsubpageTmep, args) {
        return (function () {
            //点击页码
            totalsubpageTmep.on("click", "a.page_num", function (event) {
                var current = parseInt($(this).text());
                ms.fillHtml(totalsubpageTmep, {"currPage": current, "totalPage": args.totalPage, "turndown": args.turndown});
                $('.page > a.page_num').each(function (i, e) {
                    if (parseInt($(e).text()) == current) {
                        $(e).addClass('curr');

                    }
                });
                if (typeof (args.backFn) == "function") {
                    args.backFn(current);
                }
                totalsubpageTmep.unbind('click');
            });
            //点击上一页
            totalsubpageTmep.on("click", "a.page_prev", function (event) {
                var current = parseInt($('.curr').text());
                ms.fillHtml(totalsubpageTmep, {"currPage": current, "totalPage": args.totalPage, "turndown": args.turndown});
                $('.page > a.page_num').each(function (i, e) {
                    if (current != 1) {
                        if (parseInt($(e).text()) == current - 1) {
                            $(e).addClass('curr');

                        }
                    } else {
                        if (parseInt($(e).text()) == 1) {
                            $(e).addClass('curr');

                        }
                    }
                });
                if (typeof (args.backFn) == "function") {
                    if (current == 1) {
                        args.backFn(current);
                    } else {
                        args.backFn(current - 1);
                    }
                }
                totalsubpageTmep.unbind('click');
            });
            //点击下一页
            totalsubpageTmep.on("click", "a.page_next", function (event) {
                var current = parseInt($('.curr').text());
                ms.fillHtml(totalsubpageTmep, {"currPage": current, "totalPage": args.totalPage, "turndown": args.turndown});
                $('.page > a.page_num').each(function (i, e) {
                    if (current != args.totalPage) {
                        if (parseInt($(e).text()) == current + 1) {
                            $(e).addClass('curr');

                        }
                    } else {
                        if (parseInt($(e).text()) == args.totalPage) {
                            $(e).addClass('curr');

                        }
                    }
                });
                if (typeof (args.backFn) == "function") {
                    if (current == args.totalPage) {
                        args.backFn(current);
                    } else {
                        args.backFn(current + 1);
                    }
                }
                totalsubpageTmep.unbind('click');
            });
            //点击首页
            totalsubpageTmep.on("click", "a.page_one", function (event) {
                var current = parseInt($('.curr').text());
                ms.fillHtml(totalsubpageTmep, {"currPage": current, "totalPage": args.totalPage, "turndown": args.turndown});
                $('.page > a.page_num').each(function (i, e) {
                    if (parseInt($(e).text()) == 1) {
                        $(e).addClass('curr');

                    }
                });
                if (typeof (args.backFn) == "function") {
                    args.backFn(1);
                }
                totalsubpageTmep.unbind('click');
            });
            //点击尾页
            totalsubpageTmep.on("click", "a.page_last", function (event) {
                var current = parseInt($('.curr').text());
                ms.fillHtml(totalsubpageTmep, {"currPage": current, "totalPage": args.totalPage, "turndown": args.turndown});
                $('.page > a.page_num').each(function (i, e) {
                    if (parseInt($(e).text()) == args.totalPage) {
                        $(e).addClass('curr');

                    }
                });
                if (typeof (args.backFn) == "function") {
                    args.backFn(args.totalPage);
                }
                totalsubpageTmep.unbind('click');
            });
        })();
    }
};
$.fn.createPage = function (options) {
    ms.init(this, options);
};
